/*------------------------------------------------------*/
/*                                                      */
/* Copyright (C)	sky-mobi.com                        */
/* All rights reserved                                  */
/*                                                      */
/* Author   tristan.yang                                */
/*                                                      */
/* History  2011-5-10 created                           */
/*                                                      */
/*------------------------------------------------------*/

#ifndef __DATA_SOURCE_H__
#define __DATA_SOURCE_H__

#include "RtBase.h"
#include "RtTimeValue.h"
#include "RtUtilTemplates.h"
#include "RtHttpInterface.h"
#include "connprovider.h"

#include "database.h"
#include "source.h"
#include "rscentermgr.h"
#include "livesvrmgr.h"
#include "mepdu.h"
#include "mrpdu.h"

class CDataSourceSink
{
public:
	// report source update info
	virtual void OnSourceUpdate(const CRtString& strSkyId, const strKeyId& key, DWORD uptype, DWORD storetype) = 0;
	virtual void OnSysInfoUpdate(DWORD cpuusage, DWORD memusage) = 0;
	virtual ~CDataSourceSink() {}
};

class RT_API_EXPORT CDataSource 
: public CRtTimerWrapperSink
, public IConnProviderSink
, public CRsCenterMgrSink
, public CLiveSvrMgrSink
{
public: // from IClientModule	
	static CDataSource* Instance();
	static DWORD nihaoa;
	static DWORD GetNi();
	RtResult Initialize(const s_MGParamInfo& MgInfo, 
						const s_PGParamInfo& pgInfo, 
						CDataSourceSink* psink,
						WORD nThreadNum,
						BOOL bCheckSys = TRUE,
						BOOL bLiveSvr = TRUE);
	void Release(void);
	
	// get CSource by key
	virtual RtResult FindSourceObj(const strKeyId& dwkey, 
									CRtAutoPtr<CSource>& pSource);

	virtual void LogRequestInfo(const s_InfoMediaRequest& info);
	
	//add for log body info at 2011-12-31
	virtual void LogUploadInfo(const s_InfoClientUpload& info);

	const SourceMapType& GetAllSource(){return m_mapSource;}
	// recv msg from mr or another me
	// create or delete CSource obj
	virtual void OnMessage(CRtMessageBlock& msg);

protected: // from CRtTimerWrapperSink
	virtual void OnTimer(CRtTimerWrapper* aID);
	
protected: // from IConnProviderSink
	virtual void OnConnectionReady(RtResult aResult,
							IConnection* pConn,
							const CRtInetAddr* pAddr);
	
protected: // from CRsCenterMgrSink
	virtual void OnFecthMedia(RtResult aResult,
							const CRtString& strSkyId, 
							const strKeyId& keyId, 
							CRtMessageBlock& msg);

	virtual void OnStoreMedia(RtResult aResult, 
							const strKeyId& keyId);
	
protected: // from CLiveSvrMgrSink
	virtual void OnLiveMediaUpdate(const s_LiveUpdate& updateInfo);
	virtual void OnLiveMediaData(const strKeyId& keyId, CRtMessageBlock& msg, DWORD dwchannel);

	virtual void OnMgStreamUpdate(const strKeyId& keyId, const vecStreamInfo& refVec, DWORD uptype);

protected:	
	friend class CRtSingletonT<CDataSource>;
	CDataSource();
	virtual ~CDataSource();
	
private:
	RtResult OnFetchMediaRqstMsg(const CMrPduFetchMediaRqst& pdu);
	RtResult OnLiveAddressUpdateMsg(const CMrPduLiveAddress& pdu);

	RtResult OnCopyMediaRqstMsg(const CMePduCopyMediaRqst& pdu);

	RtResult AddSource(strKeyId key, DWORD type, CRtAutoPtr<CSource>& psrc);
	RtResult DelSource(strKeyId key, BOOL bdelStore = FALSE);
	RtResult DelLiveSource(strKeyId key);
	RtResult DelTlvSource(strKeyId key);
	
	void OnSdpMediaUpdate(const s_LiveUpdate& updateInfo);
	void OnSdpMediaData(const strKeyId& keyId, CRtMessageBlock& msg, DWORD dwchannel);

	// mg support
	void OnTlvMediaUpdate(const s_LiveUpdate& updateInfo);
	void OnTlvMediaData(const strKeyId& keyId, CRtMessageBlock& msg);

	BOOL CheckSysLoadOk();

private:
	CDataSourceSink*			m_pDataSourceSink;
	SourceMapType				m_mapSource;//vod
	SourceMapType				m_mapSdpSrc;//live
	SourceMapType				m_mapTlvSrc;//tlv mg program
	
	// just for fetch media, not for log db
	CRtAutoPtr<CRsCenterMgr>	m_pRsCentMgr;
	s_MGParamInfo				m_mgdbAddr;

	// for vcs media save, as disk cache
	CRtAutoPtr<CRsCenterMgr>	m_pDiskStoreMgr;
	s_MGParamInfo				m_localmgdbAddr;
	
	// for log db
	CRtAutoPtr<CRsCenterMgr>	m_pLogCentMgr;
	s_PGParamInfo				m_pgdbAddr;

	CRtAutoPtr<CLiveSvrMgr>		m_pLiveSvrMgr;

	// for check source timeout
	CRtTimerWrapper				m_TimerOut;

	BOOL						m_bCheckSys;
	BOOL						m_bServiceLive;
};

typedef CRtSingletonT<CDataSource> CDataSourceSingleton;

#endif//__CLIENT_MODULE_H__
